﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessComponents.Managers
{
    class RoomTypeMgr:IRoomTypeMgr
    {
        private WaterFrontEntities wfe = new WaterFrontEntities();

        public IEnumerable<RoomType> FindAllRoomTypes()
        {
            return wfe.RoomTypes;
        }

        public virtual RoomType GetRoomType(int roomType)
        {
                return wfe.RoomTypes.SingleOrDefault(r => r.TypeCode == roomType);
        }

        public virtual bool  CheckDuplicateRoomType(string description, int typecode = 0)
        {
            bool retDuplicate = false ;

            if (typecode != 0)
            {

                if (wfe.RoomTypes.SingleOrDefault(r => r.Description == description.ToLower().Trim() && r.TypeCode != typecode) != null)
                {
                    retDuplicate = true;
                }
            }
            else
            {
                return wfe.RoomTypes.SingleOrDefault(r => r.Description.ToLower() == description.ToLower().Trim()) != null;
            }

            return retDuplicate; 
        }

        public void CreateRoom(RoomType roomType)
        {
            using(wfe = new WaterFrontEntities()){
                wfe.AddToRoomTypes(roomType);
                wfe.SaveChanges();
            }
        }

        public virtual void Add(RoomType roomType)
        {
            wfe.RoomTypes.AddObject(roomType);
        }
        public virtual void Delete(RoomType roomType)
        {
            wfe.RoomTypes.DeleteObject(roomType);
        }
        public virtual void Save()
        {
           
                wfe.SaveChanges();

        }
        public virtual string ValidateRoomTypeInfo(string description, string roomSize, string maxOccupant, string standartPrice)
        {
            string strReturn= string.Empty ;
            int intmaxOccupant;
            int intStdPrice;
                if (description == string.Empty ) 
                    strReturn = "Please enter Description" ;
                else if (description.Length >256)
                    strReturn = "Maximum length for description is 256";
                if (roomSize == string.Empty )
                    strReturn = Environment.NewLine + "Please enter Room Size";
                else if (roomSize.Length > 256)
                    strReturn = "Maximum length for Room Size is 256";
                if (maxOccupant == string.Empty)
                    strReturn = Environment.NewLine + "Please enter Maximum Occupants";
                else
                {
                    bool res = int.TryParse(maxOccupant, out intmaxOccupant);
                    if (res == true)
                    {
                        intmaxOccupant = Convert.ToInt32(maxOccupant);
                        if (intmaxOccupant <= 0)
                            strReturn = Environment.NewLine + "Please enter valid Maximum Occupants";
                    }
                    else
                    {
                        strReturn = Environment.NewLine + "Please enter valid Maximum Occupants";
                    }
                  
                }
                if (standartPrice == string.Empty)
                    strReturn = Environment.NewLine + "Please enter Standard Prize";
                else
                {
                    bool res = int.TryParse(standartPrice, out intStdPrice );
                    if (res == true)
                    {
                        intStdPrice = Convert.ToInt32(standartPrice);
                        if (intStdPrice <= 0)
                            strReturn = Environment.NewLine + "Please enter valid Standard Price";
                    }
                    else
                    {
                        strReturn = Environment.NewLine + "Please enter valid Standard Price";
                    }
                }
            return strReturn;
        }

        public virtual bool IsValidExtension(string fileName)
        {
            string strExtension = fileName.Substring(fileName.IndexOf("."), fileName.Length - fileName.IndexOf(".")).ToLower() ;
            if (strExtension == ".jpeg" || strExtension == ".gif" || strExtension == ".png" || strExtension == ".jpg")
                return true;
            else
                return false;
               
        }

        public virtual bool IsValidFileSize(int fileSize)
        {
            //prevent users to upload file with size more than 100 KB
            if (fileSize > 102400)
                return false;

            else
                return  true;
        }
    }
}
